package org.biodas.jdas.client;


import org.biodas.jdas.exceptions.DASClientException;
import org.biodas.jdas.schema.stylesheet.DASSTYLE;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: ljgarcia
 * Date: 11-Apr-2011
 * Time: 16:40:58
 * To change this template use File | Settings | File Templates.
 */
public class StyleSheetClient  {
    /**
     * Retrieves the sources for a data source.
     * @return
     * @throws java.net.MalformedURLException
     * @throws javax.xml.bind.JAXBException
     */
    public DASSTYLE fetchData(String serverURL) throws DASClientException {
        String url;
        if (!serverURL.endsWith("/")) {
            url = serverURL + "/stylesheet";
        } else {
            url = serverURL + "stylesheet";
        }

        JAXBContext jc;
		try {
			jc = JAXBContext.newInstance("org.biodas.jdas.schema.stylesheet");
		
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            DASSTYLE dasStyleSheet;
            dasStyleSheet = (DASSTYLE) unmarshaller.unmarshal(new URL(url));
            return dasStyleSheet;
		} catch (JAXBException e) {
			throw new DASClientException(e.getMessage(), e.getCause());
    } catch (MalformedURLException e) {
		
    	throw new DASClientException(e.getMessage(), e.getCause());
	}
    }
}
